
<template>
  <div>
    <el-row style="margin-top:20px" type="flex" justify="center">
      <el-col :span="23">
        <el-form ref="searchForm" :model="searchForm" label-width="80px" :inline="true">
          <el-form-item label="用户名称" prop="username">
            <el-input v-model="searchForm.username"></el-input>
          </el-form-item>
          <el-form-item label="手机号" prop="phone">
            <el-input v-model="searchForm.phone"></el-input>
          </el-form-item>
          <el-button size="small" type="primary" @click="init">搜索</el-button>
          <el-button size="small" @click="resetSearch('searchForm')">重置</el-button>
        </el-form>
      </el-col>
    </el-row>

    <el-row type="flex" justify="center" style="margin-bottom:10px">
      <el-col :span="23">
        <!-- <el-button size="small" icon="el-icon-plus" type="primary" @click="addUser">添加用户</el-button> -->
        <!-- <el-button size="small" icon="el-icon-minus" type="danger" @click="deleteUsers">批量删除</el-button> -->
      </el-col>
    </el-row>

    <el-row type="flex" justify="center">
      <el-col :span="23">
        <el-alert title type="success" :closable="false" show-icon   class="alert_bottom">
          已选择
          <span>{{0}}</span> 项
          <a @click="clearSelectAll" style="color:red">清空</a>
        </el-alert>

        <el-table
          size="small"
          ref="multipleTable"
          :border="false"
          :data="tableData"
          tooltip-effect="dark"
          style="width:100%"
          height="500"
          :highlight-current-row="true"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55"></el-table-column>
          <el-table-column prop="username" label="用户名称"></el-table-column>
          <el-table-column prop="headImage" label="头像">
            <template slot-scope="scope">
              <el-avatar size="small" :src="scope.row.headImage"></el-avatar>
            </template>
          </el-table-column>
          <el-table-column prop="phone" label="手机号" show-overflow-tooltip></el-table-column>
          <el-table-column prop="email" label="邮箱" show-overflow-tooltip></el-table-column>
          <el-table-column prop="gender" label="性别" show-overflow-tooltip></el-table-column>
          <el-table-column prop="createTime" label="创建时间"></el-table-column>
          <el-table-column prop="updateTime" label="更新时间"></el-table-column>
          <el-table-column label="操作" width="200">
            <template slot-scope="scope">
              <el-button @click="editUser(scope.row)" type="primary" size="mini">编辑</el-button>
              <!-- <el-button type="danger" size="mini">删除</el-button> -->
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>

    <el-row type="flex" justify="end">
      <el-col :span="8">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="searchForm.pageNumber"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="searchForm.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        ></el-pagination>
      </el-col>
    </el-row>

    <el-dialog
      top="6vh"
      :title="titleText"
      :visible.sync="dialogVisible"
      width="32%"
      :before-close="closeDialog"
    >
      <el-row type="flex" justify="center">
        <el-col :span="22">
          <el-form ref="form" :model="form" label-width="80px">
            <el-form-item label="用户名">
              <el-input v-model="form.username" style="width:300px"></el-input>
            </el-form-item>
            <el-form-item label="邮箱">
              <el-input v-model="form.email" style="width:300px"></el-input>
            </el-form-item>
            <el-form-item label="手机号">
              <el-input v-model="form.phone" style="width:300px"></el-input>
            </el-form-item>
            <el-form-item label="性别" style="width:300px">
              <el-radio-group v-model="form.gender">
                <el-radio :label="0">男</el-radio>
                <el-radio :label="1">女</el-radio>
              </el-radio-group>
            </el-form-item>

            <el-form-item label="角色分配">
              <el-select v-model="form.roleIds" multiple placeholder="请选择" style="width:300px">
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>

            <el-form-item label="用户描述">
              <el-input type="textarea" v-model="form.desc"></el-input>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
      <el-row type="flex" justify="end" style="margin-top:20px">
        <el-col :span="7">
          <el-button size="small" @click="closeDialog">取消</el-button>
          <el-button size="small" @click="submit" type="primary">提交</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { findUserByCondition, submitUser } from "@/api/system/user-api";
export default {
  /* eslint-disable */
  name: "user",
  data() {
    return {
      //edit / add
      titleText: "编辑",
      dialogVisible: false,
      options: [
        {
          value: 1,
          label: "admin"
        },
        {
          value: 2,
          label: "editor"
        }
      ],
      value1: "",

      // page
      total: 0,
      searchForm: {
        username: "",
        phone: "",
        pageNumber: 0,
        pageSize: 10
      },
      form: {
        name: "",
        phone: "",
        roleIds: []
      },
      tableData: []
    };
  },

  methods: {
    addUser() {},
    deleteUsers() {},
    clearSelectAll() {},
    handleSelectionChange() {},
    editUser(row) {
      this.form = row;
      this.dialogVisible = true;
    },
    init() {
      findUserByCondition(this.searchForm).then(response => {
        if (response.msg === "success") {
          this.total = response.data.totalElements;
          this.tableData = response.data.content;
        }
      });
    },

    //重置
    resetSearch(formName) {
      this.$refs[formName].resetFields();
      this.init();
    },
    //page
    handleSizeChange(pageSize) {
      this.searchForm.pageSize = pageSize;
      this.init();
    },
    handleCurrentChange(pageNumber) {
      this.searchForm.pageNumber = pageNumber;
      this.init();
    },

    closeDialog() {
      this.dialogVisible = false;
    },

    submit() {
      submitUser(this.form).then(response => {
        if (response.msg === "success") {
          this.$message({ message: "提交成功", type: "success" });
          this.closeDialog();
        }
      });
    }
  },

  created() {
    this.init();
  }
};
</script>

<style scoped>
/* eslint-disable */
.alert_bottom {
  margin-bottom: 20px;
}
/* eslint-disable */
</style>